 /**
 * 逻辑、功能相关描述:
 *
 * @author yandawei 编写日期：2008-10-13
 */
package com.ce.pms.common.aop.impl;

import org.apache.log4j.Logger;

import com.ce.pms.common.annotation.BuildingQuery;
import com.ce.pms.common.annotation.NoDACL;
import com.ce.util.CurrentUserThread;

/**
 * @author yandawei
 *
 */
public aspect BuildingQueryAdvice {


	private Logger log =  Logger.getLogger(BuildingQueryAdvice.class);

	public pointcut buildingQuery(BuildingQuery anno) : execution(* *(..)) && @annotation(anno);

	public pointcut noDacl() : execution(* *(..)) && @annotation(NoDACL);

	public pointcut cflowbelowBuildingQuery(BuildingQuery anno) : cflowbelow(buildingQuery(anno)) && !cflow(noDacl());

	pointcut sessionCreateQuery(String sql): execution(* createQuery(String,..)) && args(sql);

	Object around(BuildingQuery buildingQuery, String hql): sessionCreateQuery(hql) && cflowbelowBuildingQuery(buildingQuery){
		log.info("===DACL===");
		Long userId = CurrentUserThread.get().getUserId();

		String alias = buildingQuery.alias();

		String beginStr = buildingQuery.beginStr();

		String endStr = buildingQuery.endStr();

		String insertAfterPoint = buildingQuery.insertAfterPoint();

		String BuildingId = buildingQuery.property4BuildingId();


		int startPos = hql.indexOf(insertAfterPoint);
		int tempLength = startPos+insertAfterPoint.length();
		String begin = hql.substring(0,tempLength);
		String end = hql.substring(tempLength);
		//所插入的逻辑
		StringBuffer insertQuery = new StringBuffer();
		insertQuery.append(beginStr)
			       .append(alias).append(".").append(BuildingId).append(" in (select userRight.resourceId from SecUserRelationRight userRight where userRight.typeCode='002'and userRight.secUser.userId = ").append(userId).append(")")
			       .append(endStr);
		hql = begin+insertQuery+end;
		return proceed(buildingQuery,hql);
	}
}
